<?
	$page["title"] = "Alarms";
	$page["file"] = "alarms.html";

	include "include/config.inc";
	show_header($page["title"],0,0);
?>

<?
	show_table_header_begin();
	echo "ALARMS";
 
	show_table_v_delimiter(); 

	if(!isset($triggerid))
	{
		echo "<CENTER><B>No triggerID!!!!</B><BR>Please Contact Server Adminstrator</CENTER>";
		show_footer();
		exit;
	}
	else
	{
		$result=DBselect("select expression,description,priority from triggers where triggerid=$triggerid");

		$Expression=DBget_field($result,0,0);
		$Description=DBget_field($result,0,1);
		$Priority=DBget_field($result,0,2);
	}
?>

<?
	if(isset($limit) && ($limit=="NO"))
	{
		echo "[<A HREF=\"alarms.html?triggerid=$triggerid&limit=30\">";
		echo "Show only last 100</A>]";
		$limit=" ";
	}
	else 
	{
		echo "[<A HREF=\"alarms.html?triggerid=$triggerid&limit=NO\">";
		echo "Show all</A>]";
		$limit=" limit 100";
	}

	show_table_header_end();
	echo "<br>";
?>


<?
	$Expression=explode_exp($Expression,1);
	show_table_header("$Description<BR><font size=-1>$Expression</font>");
?>

<FONT COLOR="#000000">
<?
	$sql="select clock,istrue from alarms where triggerid=$triggerid order by clock desc $limit";
	$result=DBselect($sql);

	echo "<CENTER>";
	echo "<TABLE WIDTH=100% BORDER=0 BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=3>";
	echo "<TR>";
	echo "<TD><FONT SIZE=+1>Time</FONT></TD>";
	echo "<TD><FONT SIZE=+1>Status</FONT></TD>";
	echo "<TD><FONT SIZE=+1>Length</FONT></TD>";
	echo "<TD><FONT SIZE=+1>Sum</FONT></TD>";
	echo "<TD><FONT SIZE=+1>%</FONT></TD>";
	echo "</TR>";
	$truesum=0;
	$falsesum=0;
	$dissum=0;
	$clock=mktime();
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$lclock=$clock;
		$clock=DBget_field($result,$i,0);
		$leng=$lclock-$clock;
		$status=DBget_field($result,$i,1);
		if($status==0) {  echo "<TR BGCOLOR=#EEFFEE>"; }
		elseif($status==2) { echo "<TR BGCOLOR=#DDDDDD>"; }
		elseif($status==3) { echo "<TR BGCOLOR=#EEEEEE>"; }
		else { echo "<TR BGCOLOR=#FFDDDD>"; }

		echo "<TD>",date("Y.M.d H:i:s",$clock),"</TD>";
		if($status==1)
		{
			$istrue="TRUE";
			$truesum=$truesum+$leng;
			$sum=$truesum;
		}
		elseif($status==0)
		{
			$istrue="FALSE";
			$falsesum=$falsesum+$leng;
			$sum=$falsesum;
		}
		elseif($status==2)
		{
			$istrue="DISABLED";
			$dissum=$dissum+$leng;
			$sum=$dissum;
		}
		elseif($status==3)
		{
			$istrue="MODIFIED";
			$dissum=$dissum+$leng;
			$sum=$dissum;
		}
	
		$proc=(100*$sum)/($falsesum+$truesum+$dissum);
		$proc=round($proc*100)/100;
		$proc="$proc%";
 
		echo "<TD><B>",$istrue,"</B></TD>";
		if($leng>60*60*24)
		{
			$leng= round(($leng/(60*60*24))*10)/10;
			$leng="$leng days";
		}
		elseif ($leng>60*60)
		{
			$leng= round(($leng/(60*60))*10)/10;
			$leng="$leng hours"; 
		}
		elseif ($leng>60)
		{
			$leng= round(($leng/(60))*10)/10;
			$leng="$leng mins";
		}
		else
		{
			$leng="$leng secs";
		}

		if($sum>60*60*24)
		{
			$sum= round(($sum/(60*60*24))*10)/10;
			$sum="$sum days";
		}
		elseif ($sum>60*60)
		{
			$sum= round(($sum/(60*60))*10)/10;
			$sum="$sum hours"; 
		}
		elseif ($sum>60)
		{
			$sum= round(($sum/(60))*10)/10;
			$sum="$sum mins";
		}
		else
		{
			$sum="$sum secs";
		}
  
		echo "<TD>$leng</TD>";
		echo "<TD>$sum</TD>";
		echo "<TD>$proc</TD>";
		echo "</TR>";
	}
	echo "</TABLE><BR>";
?>
</FONT>
</TR>
</TABLE></CENTER>

<?
	show_footer();
?>
